package cloudPayAdmin.admin.service.hicatcard.user;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cloudPayAdmin.admin.dbapp.entity.hicatcard.user.TUserPlan;
import cloudPayAdmin.admin.dbapp.entity.hicatcard.user.TUserThird;
import cloudPayAdmin.admin.dbapp.repo.hicatcard.user.UserPlanRepo;
import cloudPayAdmin.admin.dbapp.repo.hicatcard.user.UserThirdRepo;
import cloudPayAdmin.util.TimeUtil;

@Component
@Transactional
public class UserThirdService {
	
	@Autowired
	UserThirdRepo userThirdRepo;
	
	@Autowired
	UserPlanRepo userPlanRepo;
	
	@Transactional(readOnly=true)
	public List<TUserPlan> findUserPlanByType(Integer type) {
		return userPlanRepo.findByType(TUserThird.TYPE_RENBAO);
	}
	
	@Transactional(readOnly=true)
	public List<TUserThird> findUserThirdByAccountIds(List<String> accountIds) {
		return userThirdRepo.findByAccountIds(accountIds);
	}
	
	@Transactional(readOnly=true)
	public String getMaxAccountId() {
		return userThirdRepo.getMaxAccountId();
	}
	
	@Transactional(readOnly=true)
	public Integer getMaxBatch() {
		return userThirdRepo.getMaxBatch();
	}
	
	public List<TUserThird> saveUserThird(List<TUserThird> userThirdList,String prefix) {
		
		List<TUserPlan> userPlanLisst = userPlanRepo.findByType(TUserThird.TYPE_RENBAO);
		if(CollectionUtils.isNotEmpty(userPlanLisst)) {
			
			TUserPlan oriPlan = userPlanLisst.get(0);
			TUserPlan newPlan = new TUserPlan();
			newPlan.setType(oriPlan.getType());
			newPlan.setCompanyName(oriPlan.getCompanyName());
			newPlan.setPrefix(prefix);
			newPlan.setStart(oriPlan.getStart());
			newPlan.setNumber(userThirdList.size());
			newPlan.setBackGround(oriPlan.getBackGround());
			newPlan.setLogo(oriPlan.getLogo());
			newPlan.setStatus(oriPlan.getStatus());
			newPlan.setCreateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			newPlan.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			userPlanRepo.save(newPlan);
		}
		
		return userThirdRepo.save(userThirdList);
	}
}
